package com.newedu.equipmgr.servlet.equipment.area;

import com.alibaba.fastjson.JSONObject;
import com.newedu.equipmgr.service.AreaService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author jerry
 * @create 2020-06-08 16:36:24
 * @company 新开普电子股份有限公司
 */
@WebServlet(name="AreaDeleteServlet", urlPatterns = {"/api/servlet/area/delete"})
public class AreaDeleteServlet extends HttpServlet {
    //alt+insert
    // ctrl +o

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doPost(req, resp);

        String id = req.getParameter("id");

        if(id!=null){
            AreaService areaService = new AreaService();

            JSONObject jsonObject = new JSONObject();
            try{
                boolean delete = areaService.delete(Integer.parseInt(id));
                if(delete){
                    jsonObject.put("status","success");
                    jsonObject.put("message","操作成功");
                }else{
                    jsonObject.put("status","success");
                    jsonObject.put("message","操作成功");
                }
            }
            catch (Exception e){
                e.printStackTrace();
                jsonObject.put("status","fail");
                jsonObject.put("message",e.getMessage());
            }

            resp.setContentType("application/json;charset=utf-8");
            String jsonString = jsonObject.toJSONString();
            System.out.println(jsonString);
            resp.getWriter().println(jsonString);


        }
    }
}
